package main.java.Learn.reflcet;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.HashMap;
import java.util.Map;

public class Reflect{
    private final Object obj;
    public Reflect(Object obj){
        this.obj = obj;
    }

    public Map<String,Object> getObjFieldsValues() throws IllegalAccessException {
        HashMap<String, Object> map = new HashMap<>();
        Class<?> aClass = obj.getClass();
        Field[] fields = aClass.getDeclaredFields();
        for(Field field:fields){
            field.setAccessible(true);
            Object o = field.get(obj);
            map.put(field.getName(),o);
        }
        return map;
    }

    public Map<String,String> getObjMethodsDetails(){
        Class<?> aClass = obj.getClass();
        HashMap<String, String> map = new HashMap<>();
        Method[] declaredMethods = aClass.getDeclaredMethods();
        for(Method method:declaredMethods){
            method.setAccessible(true);
            String name = method.getName();
            Parameter[] parameters = method.getParameters();
            StringBuilder pars = new StringBuilder();
            for(Parameter parameter:parameters){
                pars.append(parameter.getType());
            }
            map.put(name,pars.toString());
        }
        return map;
    }
}
